#ifndef XX_COMMON_SYSTEM_HEAD_HH
#define XX_COMMON_SYSTEM_HEAD_HH


#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <math.h>
#include <errno.h>
#include <ctype.h>

typedef char 			int8;
typedef char 			int24[3];
typedef short 			int16;
typedef int 			int32;

typedef unsigned char 	uint8;
typedef unsigned short 	uint16;
typedef unsigned char 	uint24[3];
typedef unsigned int 	uint32;


#ifdef	WIN32

#include <ws2tcpip.h>
#include <ws2ipdef.h>

#include <MSWSock.h>
#include <winsock2.h>
#include <mmsystem.h>

#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <malloc.h>
#include <time.h>
#include <process.h>
#include <iphlpapi.h>
#include <assert.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "Iphlpapi.lib")

typedef SOCKET					socket_t;
typedef __int64					int64;
typedef unsigned __int64		uint64;

#define strcasecmp 				_stricmp
#define strncasecmp 			_memicmp

#else

#include <sys/types.h>
#include <sys/ipc.h>
#include <semaphore.h>
#include <signal.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/epoll.h>

#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>

#include <arpa/inet.h>
#include <netdb.h>

#include <net/if.h>
#include <sys/resource.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>

#include <ctype.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>

#include <dlfcn.h>
#include <dirent.h>
#include <ifaddrs.h>
#include <sys/prctl.h>

typedef int							socket_t;
typedef long long					int64;
typedef unsigned long long			uint64;

#define INFINITE					-1
#define	SOCKET_ERROR				-1
#define INVALID_SOCKET				-1
#define WSAGetLastError()          errno

#endif


#define MAX_BUFFER_16KSZ					0x4000
#define MAX_BUFFER_32KSZ					0x8000
#define MAX_BUFFER_64KSZ					0x10000
#define MAX_BUFFER_128KSZ					0x20000
#define MAX_BUFFER_256KSZ					0x40000
#define MAX_BUFFER_512KSZ					0x80000
#define MAX_BUFFER_1MSIZE					0x100000
#define MAX_BUFFER_2MSIZE					0x200000
#define MAX_BUFFER_4MSIZE					0x400000


#define XX_ALIGN16(a)                      (((a + 31) >> 4) << 4) // round up to a multiple of 16
#define XX_ALIGN32(a)                      (((a + 63) >> 5) << 5) // round up to a multiple of 32

#define xx_min(a, b)						(a < b) ? a : b
#define xx_max(a, b)						(a > b) ? a : b
#define xx_div(a, b)						(a + b - 1) / b
#define sizeof_array(a)					   (sizeof(a)/sizeof(a[0]))

#define SAFE_FREE(P)						if (P){free(P); P = NULL;}
#define SAFE_DELETE(P)                      if (P){delete P; P = NULL;}
#define SAFE_DELETEA(P)                     if (P){delete[] P; P = NULL;}
#define SAFE_CLOSEHD(P)						if (P){CloseHandle(P); P = NULL;}


#endif /* #ifndef _INTERNAL_H_ */
